package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import service.tAreaService;
import service.tDeviceService;
import service.tUserService;
import entity.tArea;
import entity.tDevice;
import entity.tUser;

public class GettDeviceDateToAlter {
	
	private String userName;
	private String userId;
	private String deviceId;
	private String deviceNo;
	private String deviceName;
	private Timestamp addTime;
	private String deviceArea;
	private String place;
	private String deviceDescribe;
	private String mac;
	
	private int ph;
	private int orp;
	private int temperature;
	private int ec;
	private int openEC;
	private int closeEC;
	
	private int esil;
	private int ec1;
	private int ec2;
	private int ec3;
	private int ec4;
	private int ec5;
	private int ec6;
	private int ec7;
	private int ec8;
	private int ec9;
	private int ec10;
	private int ec11;
	private int ec12;
	private int ec13;
	
	private tDeviceService tdeviceService;
	private tUserService tuserService;
	private tAreaService tareaService;
	
	public String get() throws IOException, ParseException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		System.out.println("----------经过GettDeviceDateToAlter----------");
		
		request.getAttribute("deviceId");
		System.out.println(deviceId);
		
		List<tDevice> tdevice = tdeviceService.searchById(deviceId);
		
		deviceId = tdevice.get(0).getDeviceId();
		deviceNo = tdevice.get(0).getDeviceNo();
		deviceName = tdevice.get(0).getDeviceName();
		addTime = tdevice.get(0).getAddTime();
		deviceArea = tdevice.get(0).getDeviceArea();
		place = tdevice.get(0).getPlace();
		deviceDescribe = tdevice.get(0).getDeviceDescribe();
		mac = tdevice.get(0).getMac();
		
		closeEC = tdevice.get(0).getCloseEC();
		
		ph = tdevice.get(0).getPh();
		orp = tdevice.get(0).getOrp();
		temperature = tdevice.get(0).getTemperature();
		ec = tdevice.get(0).getEc();
		openEC = tdevice.get(0).getOpenEC();
		
		esil = tdevice.get(0).getEsil();
	    ec1 = tdevice.get(0).getEc1();
	    ec2 = tdevice.get(0).getEc2();
	    ec3 = tdevice.get(0).getEc3();
	    ec4 = tdevice.get(0).getEc4();
	    ec5 = tdevice.get(0).getEc5();
	    ec6 = tdevice.get(0).getEc6();
	    ec7 = tdevice.get(0).getEc7();
	    ec8 = tdevice.get(0).getEc8();
	    ec9 = tdevice.get(0).getEc9();
	    ec10 = tdevice.get(0).getEc10();
	    ec11 = tdevice.get(0).getEc11();
	    ec12 = tdevice.get(0).getEc12();
	    ec13 = tdevice.get(0).getEc13();
	    
		
		List<tArea> lists = new ArrayList();
		JSONArray jsonArray = new JSONArray();
		
		List<tArea> tarea = new ArrayList<tArea>();
		
		tarea = tareaService.searchAll();
		System.out.println(tarea);
		 for (tArea tareas : tarea){
			JSONObject t = new JSONObject();
			
			String name = tareas.getName();
			System.out.println(name);
			System.out.println(deviceArea);
			if (! name.equals(deviceArea)){
				
				t.put("name", tareas.getName());
				System.out.println(t);
				
				lists.add(tareas);
			}
			
			//jsonArray.add(t);
			
			
		 }
		 
		 request.setAttribute("lists",lists);


		
		System.out.println(tdevice.get(0).getDeviceName());
		System.out.println(tdevice.get(0).getDeviceDescribe());
		System.out.println(tdevice.get(0).getMac());
		
		return "success";
	}
	
	public String gettoallot() throws IOException, ParseException{
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		System.out.println("----------经过GettDeviceDateToAlter----------");
		
		request.getAttribute("deviceId");
		System.out.println(deviceId);
		request.getAttribute("userId");
		System.out.println("userId:"+userId);
		
		
		
		if(userId == "" || userId == null || userId.equals("")){
			JSONArray jsonArray = new JSONArray();
			List<tUser> lists = new ArrayList<tUser>();
			lists = tuserService.searchAlltUser();
			for(tUser tuser: lists){
				JSONObject t = new JSONObject();
				t.put("userName", tuser.getUserName());
				jsonArray.add(t);
				System.out.println(t);	
			}
			request.setAttribute("lists",lists);
		}
		else{
			JSONArray jsonArray = new JSONArray();
			List<tUser> lists = new ArrayList<tUser>();
			
			lists = tuserService.searchById(userId);
			for(tUser tuser: lists){
				JSONObject t = new JSONObject();
				t.put("userName", tuser.getUserName());
				jsonArray.add(t);
				System.out.println(t);	
			}
			
			//System.out.println(lists.get(0).getUserName());
			
			request.setAttribute("lists",lists);
			
		}
		
		System.out.println(userName);
		
		List<tDevice> tdevice = tdeviceService.searchById(deviceId);
		
		
		
		deviceId = tdevice.get(0).getDeviceId();
		deviceNo = tdevice.get(0).getDeviceNo();
		deviceName = tdevice.get(0).getDeviceName();
		deviceDescribe = tdevice.get(0).getDeviceDescribe();
		
		System.out.println(tdevice.get(0).getDeviceName());
		System.out.println(tdevice.get(0).getDeviceDescribe());
		System.out.println(tdevice.get(0).getMac());
		
		return "success";
		
	}

	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getDeviceNo() {
		return deviceNo;
	}

	public void setDeviceNo(String deviceNo) {
		this.deviceNo = deviceNo;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}
	

	public Timestamp getAddTime() {
		return addTime;
	}

	public void setAddTime(Timestamp addTime) {
		this.addTime = addTime;
	}

	public String getDeviceArea() {
		return deviceArea;
	}

	public void setDeviceArea(String deviceArea) {
		this.deviceArea = deviceArea;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getDeviceDescribe() {
		return deviceDescribe;
	}

	public void setDeviceDescribe(String deviceDescribe) {
		this.deviceDescribe = deviceDescribe;
	}

	public String getMac() {
		return mac;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public tDeviceService getTdeviceService() {
		return tdeviceService;
	}

	public void setTdeviceService(tDeviceService tdeviceService) {
		this.tdeviceService = tdeviceService;
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getPh() {
		return ph;
	}

	public void setPh(int ph) {
		this.ph = ph;
	}

	public int getOrp() {
		return orp;
	}

	public void setOrp(int orp) {
		this.orp = orp;
	}

	public int getTemperature() {
		return temperature;
	}

	public void setTemperature(int temperature) {
		this.temperature = temperature;
	}

	public int getEc() {
		return ec;
	}

	public void setEc(int ec) {
		this.ec = ec;
	}

	public int getOpenEC() {
		return openEC;
	}

	public void setOpenEC(int openEC) {
		this.openEC = openEC;
	}

	public int getCloseEC() {
		return closeEC;
	}

	public void setCloseEC(int closeEC) {
		this.closeEC = closeEC;
	}

	public tAreaService getTareaService() {
		return tareaService;
	}

	public void setTareaService(tAreaService tareaService) {
		this.tareaService = tareaService;
	}

	public int getEsil() {
		return esil;
	}

	public void setEsil(int esil) {
		this.esil = esil;
	}

	public int getEc1() {
		return ec1;
	}

	public void setEc1(int ec1) {
		this.ec1 = ec1;
	}

	public int getEc2() {
		return ec2;
	}

	public void setEc2(int ec2) {
		this.ec2 = ec2;
	}

	public int getEc3() {
		return ec3;
	}

	public void setEc3(int ec3) {
		this.ec3 = ec3;
	}

	public int getEc4() {
		return ec4;
	}

	public void setEc4(int ec4) {
		this.ec4 = ec4;
	}

	public int getEc5() {
		return ec5;
	}

	public void setEc5(int ec5) {
		this.ec5 = ec5;
	}

	public int getEc6() {
		return ec6;
	}

	public void setEc6(int ec6) {
		this.ec6 = ec6;
	}

	public int getEc7() {
		return ec7;
	}

	public void setEc7(int ec7) {
		this.ec7 = ec7;
	}

	public int getEc8() {
		return ec8;
	}

	public void setEc8(int ec8) {
		this.ec8 = ec8;
	}

	public int getEc9() {
		return ec9;
	}

	public void setEc9(int ec9) {
		this.ec9 = ec9;
	}

	public int getEc10() {
		return ec10;
	}

	public void setEc10(int ec10) {
		this.ec10 = ec10;
	}

	public int getEc11() {
		return ec11;
	}

	public void setEc11(int ec11) {
		this.ec11 = ec11;
	}

	public int getEc12() {
		return ec12;
	}

	public void setEc12(int ec12) {
		this.ec12 = ec12;
	}

	public int getEc13() {
		return ec13;
	}

	public void setEc13(int ec13) {
		this.ec13 = ec13;
	}
	

}
